package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Item;
import cn.wolfcode.luowowo.article.domain.ItemComment;
import cn.wolfcode.luowowo.article.service.IItemCommentService;
import cn.wolfcode.luowowo.article.service.IItemService;
import cn.wolfcode.luowowo.comment.domain.UserInfoItem;
import cn.wolfcode.luowowo.comment.service.IUserInfoItemService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.web.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@RequestMapping("/itemshop")
@Controller
public class ItemshopController {
    @Reference
    private IItemService itemService;
    @Reference
    private IUserInfoItemService userInfoItemService;
    @Reference
    private IItemCommentService itemCommentService;
    @RequestMapping("")
    public String list(Model model, @UserParam UserInfo userInfo) {
        QueryObject qo = new QueryObject();
        Page<Object> page = new Page<>();
        PageInfo pageInfo = itemService.list(qo);
        model.addAttribute("pageInfo", pageInfo);
        if(userInfo != null){
            UserInfoItem userInfoItem = userInfoItemService.get(userInfo.getId());
            model.addAttribute("userInfo", userInfoItem);
        }
        return "/itemshop/list";
    }

    @RequestMapping("/myItem")
    public String myItem(Model model, @UserParam UserInfo userInfo) {
        UserInfoItem userInfoItem = userInfoItemService.get(userInfo.getId());
        Map<Long, Integer> itemNum = userInfoItem.getItemNum();
        List<Item> list = new ArrayList<>();
        if (itemNum != null) {
            for (Long itemId : itemNum.keySet()) {
                Item item = itemService.get(itemId);
                item.setNum(Long.parseLong(itemNum.get(itemId).toString()));
                list.add(item);
            }
        }
        QueryObject queryObject = new QueryObject();
        PageHelper.startPage(queryObject.getCurrentPage(), queryObject.getPageSize());
        PageInfo pageInfo = new PageInfo(list);
        if (pageInfo.getSize() != 0) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "/itemshop/myItemTempl";
    }
    @RequestMapping("/shopItem")
    public String shopItem(Model model, @ModelAttribute("qo")QueryObject qo){
        PageInfo pageInfo = itemService.list(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/itemshop/itemTempl";
    }
    @RequestMapping("/buy")
    @ResponseBody
    @RequireLogin
    public AjaxResult buy(Model model,Long id,@UserParam UserInfo userInfo){
        UserInfoItem userInfoItem = userInfoItemService.get(userInfo.getId());
        Item item = itemService.get(id);
        if(item.getNum() == 0){
            return new AjaxResult(false);
        }
        if(item.getScore()>userInfoItem.getScore()){
            return new AjaxResult(false);
        }
        userInfoItem.setScore(userInfoItem.getScore()-item.getScore());
        Map<Long, Integer> map = userInfoItem.getItemNum();
        if(map !=null){
            Integer num = map.get(item.getId());
            if(num == null){
                num = 0;
            }
            map.put(item.getId(),num+1);
        }else{
            map = new HashMap<>();
            map.put(item.getId(),1);
        }
        userInfoItem.setItemNum(map);
        userInfoItemService.saveOrUpdate(userInfoItem);

        item.setNum(item.getNum()-1);
        itemService.update(item);
        return AjaxResult.success();
    }
    @RequestMapping("/addComment")
    @RequireLogin
    public String addComment(Model model,String context,@UserParam UserInfo userInfo,@ModelAttribute("qo")QueryObject qo){
        ItemComment itemComment = new ItemComment();
        itemComment.setComment(context);
        itemComment.setUid(userInfo.getId());
        itemComment.setUname(userInfo.getNickname());
        itemComment.setTime(new Date());
        itemComment.setImg(userInfo.getHeadImgUrl());
        itemCommentService.save(itemComment);
        PageInfo pageInfo =  itemCommentService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/itemshop/commentTempl";
    }
    @RequestMapping("/commentList")
    public String commentList(Model model){
        QueryObject qo = new QueryObject();
        PageInfo pageInfo = itemCommentService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/itemshop/commentTempl";
    }
}
